﻿#include "socket.h"
#include <iostream>
int main() 
{
    Socket socket;
    socket.createTcpServer(58888);
    while(true)
    {
        int client_fd = socket.accept();
        if(client_fd < 0)
        {
            continue;
        }
        std::cout << "client connected, client_fd: " << client_fd << std::endl;
        // 创建一个客户端套接字，通信一次断开
        Socket client_socket(client_fd);
        char buf[1024];
        ssize_t ret = client_socket.recv(buf, sizeof(buf));
        if(ret < 0)
        {
            std::cout << "client disconnected, client_fd: " << client_fd << std::endl;
            break;
        }
        buf[ret] = '\0';
        std::cout << "recv: " << buf << std::endl;
        std::string response = "server received: " + std::string(buf);
        client_socket.send(response.c_str(), response.size());
        client_socket.close();
    }
    socket.close();
    return 0;
}